import java.awt.*;
import java.awt.event.*;
import java.util.*;
class PlotControlPanel extends Container implements ActionListener
{
	private Button _replot;
	private Button _store;
	private Choice _plottingmode;
	private Plot _plot;
	private Series _data;
	void setPlot(Plot plot)
	{
		_plot=plot;
	}
	Plot getPlot()
	{
		return _plot;
	}
	PlotControlPanel(Plot plot,Series data)
	{
		super();
		_data=data;
		_plot=plot;
		setLayout(new FlowLayout());
		_replot=new Button("Replot");
		_store=new Button("Store");
		_plottingmode=new Choice();
		_plottingmode.add("Temperature");
		_plottingmode.add("Magnetic Field");
		_plottingmode.add("Time");
		add(_replot);
		add(_store);
		_store.addActionListener(this);
		_replot.addActionListener(this);
	}
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==_replot)
		{
			LinkedList<Point> points=new LinkedList<Point>();
			_data.plot2D(points,_plottingmode.getSelectedIndex());
			_plot.plot(points);
			_plot.repaint();
		}
		else if(ae.getSource()==_store)
		{
		}
	}
}
class SeriesControlPanel extends Container implements ActionListener
{
	private Series _data;
	private Button _calculate;
	private Button _store;
	private Plot _plot;
	private Button _setMeasurementProperties;
	private SeriesSettingDialog _settingsdialog;
	void clearPoints()
	{
	}
	void storeData(String filename)
	{
	}
	void createStoreDialogFrame()
	{
	}
	void createSeriesDialogFrame()
	{
	}
	void setPlot(Plot plot)
	{
		_plot=plot;
	}
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==_calculate)
		{
			_data.calculate();
		} else if(ae.getSource()==_store)
		{
		} else if(ae.getSource()==_setMeasurementProperties)
		{
			_settingsdialog.setVisible(true);
		}
	}
	Plot getPlot()
	{
		return _plot;
	}
	SeriesControlPanel(Plot plot,Series data,SeriesSettingDialog seriessetting)
	{
		super();
		_data=data;
		_plot=plot;
		_store=new Button("Store");
		_calculate=new Button("Calculate");
		_setMeasurementProperties=new Button("Options");
		setLayout(new FlowLayout());
		add(_store);
		add(_calculate);
		add(_setMeasurementProperties);
		_store.addActionListener(this);
		_calculate.addActionListener(this);
		_setMeasurementProperties.addActionListener(this);
		_settingsdialog=seriessetting;
	}
}
class IFrame extends Frame
{
	private Plot _plot;
	private SeriesControlPanel _seriesControl;
	private PlotControlPanel _plotControl;
	private SeriesSettingDialog _seriessetting;
	IFrame(Plot plot,Series data)
	{
		_plot=plot;
		_seriessetting=new SeriesSettingDialog(this,data);
		_plotControl=new PlotControlPanel(plot,data);
		_seriesControl=new SeriesControlPanel(plot,data,_seriessetting);
		setLayout(new BorderLayout());
		add(_plotControl,BorderLayout.NORTH);
		add(_plot,BorderLayout.CENTER);
		add(_seriesControl,BorderLayout.SOUTH);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we)
			{
				System.exit(0);
			}
		});
		setSize(700,700);
		setTitle("Ising Model study utility");
		setVisible(true);
	}
	public static void main(String[] args)
	{
		Series data=new Series(null);
		int length,width;
		length=Integer.valueOf(args[0]).intValue();
		width=Integer.valueOf(args[1]).intValue();
		data.initSeries(length,width);
		Plot plot=new Plot(500,500,0,20000,0,20000,30,30);
		IFrame mainframe=new IFrame(plot,data);
	}
}
class DuoContainer extends Container
{
	private Component _first;
	private Component _second;
	DuoContainer(Component first,Component second)
	{
		super();
		_first=first;
		_second=second;
		setLayout(new FlowLayout());
		add(first);
		add(second);
	}
}

class SeriesSettingDialog extends Dialog implements ActionListener
{
	private Button _cancel;
	private Button _apply;
	private Choice _fieldDependancyType;
	private Choice _algoritmeTimeDependancyType;
	private Choice _algoritmeType;
	private TextField _fieldFrequency;
	private TextField _fieldPhase;
	private Choice _seriesStepMode;
	private TextField _initialValue;
	private TextField _finalValue;
	private TextField _step;
	private Choice _argumentType;
	private Checkbox _freezeLattice;
	private Choice _initialAlignment;
	private SeriesOptions _options;
	SeriesSettingDialog(Frame owner,Series data)
	{
		super(owner);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we)
			{
				setVisible(false);
			}
		});
		_options=data._options;
		_cancel=new Button("Cancel");
		_apply=new Button("Apply");
		_fieldDependancyType=new Choice();
		_fieldDependancyType.add("Constant");
		_fieldDependancyType.add("Linear");
		_fieldDependancyType.add("Exponential");
		_fieldDependancyType.add("Sine");
		_algoritmeTimeDependancyType=new Choice();
		_algoritmeTimeDependancyType.add("Asymptotic");
		_algoritmeTimeDependancyType.add("Time-dependant");
		_algoritmeType=new Choice();
		_algoritmeType.add("Heat Bath");
		_algoritmeType.add("Metropolis");
		_algoritmeType.add("Demon");
		_fieldFrequency=new TextField(7);
		_fieldPhase=new TextField(7);
		_initialValue=new TextField(7);
		_step=new TextField(7);
		_finalValue=new TextField(7);
		_seriesStepMode=new Choice();
		_seriesStepMode.add("Linear");
		_seriesStepMode.add("Logarithmic");
		_initialAlignment=new Choice();
		_initialAlignment.add("Field-wise");
		_initialAlignment.add("Contre-field-wise");
		_initialAlignment.add("Even distribution");
		_freezeLattice=new Checkbox();
		_argumentType=new Choice();
		_argumentType.add("Temperature");
		_argumentType.add("Magnetic Field");
		_argumentType.add("Time");
		_cancel.addActionListener(this);
		_apply.addActionListener(this);
		setLayout(new GridLayout(13,1));
		add(new DuoContainer(_cancel,_apply));
		add(new DuoContainer(new Label("Algoritme"),_algoritmeType));
		add(new DuoContainer(new Label("Time-Dependancy"),_algoritmeTimeDependancyType));
		add(new DuoContainer(new Label("MagneticField"),_fieldDependancyType));
		add(new DuoContainer(new Label("Frequency"),_fieldFrequency));
		add(new DuoContainer(new Label("Phase"),_fieldPhase));
		add(new DuoContainer(new Label("Argument"),_argumentType));
		add(new DuoContainer(new Label("Series mode"),_seriesStepMode));
		add(new DuoContainer(new Label("Initial Value"),_initialValue));
		add(new DuoContainer(new Label("Step"),_step));
		add(new DuoContainer(new Label("Final Value"),_finalValue));
		add(new DuoContainer(new Label("Initial Alignment"),_initialAlignment));
		add(new DuoContainer(new Label("Freeze lattice on each point"),_freezeLattice));
		setSize(300,600);
		setTitle("Options");
	}
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==_cancel)
		{
			setVisible(false);
		} else if(ae.getSource()==_apply)
		{
			applySettings();
			setVisible(false);
		}
	}
	void applySettings()
	{
		_options._argumentType=_argumentType.getSelectedIndex();
		_options._algoritme=_algoritmeType.getSelectedIndex();
		_options._asymptotic=_algoritmeTimeDependancyType.getSelectedIndex()==0;
		_options._start=Float.valueOf(_initialValue.getText()).floatValue();
		_options._step=Float.valueOf(_step.getText()).floatValue();
		_options._end=Float.valueOf(_finalValue.getText()).floatValue();
		_options._frequency=Float.valueOf(_fieldFrequency.getText()).floatValue();
		_options._phase=Float.valueOf(_fieldPhase.getText()).floatValue();
		_options._seriesMode=_seriesStepMode.getSelectedIndex();
		_options._freeze=_freezeLattice.getState();
	}
}
